package com.fzrskj.cloud.free.sys.service;

import com.fzrskj.cloud.free.consts.AppCode;
import com.fzrskj.cloud.free.sys.bean.Field;
import com.fzrskj.cloud.free.sys.bean.Param;
import com.fzrskj.cloud.free.sys.bean.Table;
import com.fzrskj.cloud.free.sys.utils.CDicUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class CDicService extends CObject{

	/**to query table dictionary by itype
	 * @param req HttpServletRequest
	 * @param resp HttpServletResponse
	 * @param p parameter,it's format is that: p={"itype":n}
	 * @param sid session id
	 * @return json string
	 * @author lms 2016.07.26
	 */
	public String getTableDic(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int itype = JsonTool.getIntValue(p, "itype",-1);
		if (itype==-1){
			this.log(p+"-->invalid json, can't parase itype value");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CDicUtils obj = new CDicUtils();
		List<Table> tbs = obj.getTableDic(itype);
		String code = obj.getErrCode();
		obj.dispose();
		if (tbs==null){
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultData(true, code, JsonTool.listBeanToJson(tbs));
	}

	/**to query field dictionary by table code
	 * @param req HttpServletRequest
	 * @param resp HttpServletResponse
	 * @param p parameter,it's format is that: p={"code":"xxx");
	 * @param sid session id
	 * @return json string
	 * @author lms 2016.07.26
	 */
	public String getFieldDic(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String code = JsonTool.getStrValue(p, "code", "");
		if (StrTool.isBlankStr(code)){
			this.log(p+"-->invalid json, can't parase code value");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CDicUtils obj = new CDicUtils();
		List<Field> fls = obj.getFieldDic(code);
		String icode = obj.getErrCode();
		obj.dispose();
		
		if (fls==null){
			return XmlCodeParser.formatResultTips(false, icode);
		}
		return XmlCodeParser.formatResultData(true, icode, JsonTool.listBeanToJson(fls));
	}

	/**to lock table field show width 
	 * @param req HttpServletRequest
	 * @param resp HttpServletResponse
	 * @param p, parameter's format is that:p=[{"code":"xxx","name":"fieldname1","width":n,"showId":m},{"code":"xxx","name":"fieldname2","width":n,"showId":m}...}
	 * @param sid session id
	 * @return json string
	 */
	public String lockFieldDic(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		List<Field> fls = JsonTool.jsonToListBean(p, Field.class);
		if (fls==null){
			this.log(p+"-->invalid json, can't parase param to List<Field>");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CDicUtils obj = new CDicUtils();
		int n = obj.lockFieldDic(fls);
		obj.dispose();
		if (n<=0){
			return XmlCodeParser.formatResultTips(false, AppCode.DB_ERR);
		}
		return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
	}

	/**to query system parameter
	 * @param req HttpServletRequest
	 * @param resp HttpServletResponse
	 * @param p, parameter's format is that:p={"code":"xxx","type":n}
	 * @param sid session id
	 * @return json string
	 */
	public String getParam(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String code = JsonTool.getStrValue(p, "code", "");
		int type = JsonTool.getIntValue(p,"type",-1);

		CDicUtils obj = new CDicUtils();
		List<Param> ps = obj.getParam(code, type);
		String icode  = obj.getErrCode();
		obj.dispose();
		
		if (ps==null){
			return XmlCodeParser.formatResultTips(false, icode);
		}
		return XmlCodeParser.formatResultData(true, icode, JsonTool.listBeanToJson(ps));
	}

	/**to set system parameter
	 * @param req HttpServletRequest
	 * @param resp HttpServletResponse
	 * @param p, parameter's format is that:p=[{"pCode":"xxx","pValue":"value"},{"pCode":"yyy","pValue":"value"}]
	 * @param sid session id
	 * @return json string
	 */
	public String setParam(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		List<Param> params = JsonTool.jsonToListBean(p, Param.class);
		if (params==null){
			this.log(p+"-->invalid json, can't parase to Param Object");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CDicUtils obj = new CDicUtils();
	    obj.setParam(params);
		obj.dispose();
		return XmlCodeParser.formatResultTips(true,SysErr.CODE_SUCCESS);
	}

}
